package com.tsystems.skilltest;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 * @author aesipov
 */
public class TransactionListXMLConverter
{
    public String marshal(TransactionList transactionList) throws JAXBException
    {
        JAXBContext jaxbContext = JAXBContext.newInstance(TransactionList.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        StringWriter stringWriter = new StringWriter();
        jaxbMarshaller.marshal(transactionList, stringWriter);
        return stringWriter.toString();
    }

    public TransactionList unmarshal(String xml) throws JAXBException
    {
        JAXBContext jaxbContext = JAXBContext.newInstance(TransactionList.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        StringReader stringReader = new StringReader(xml);
        return (TransactionList) jaxbUnmarshaller.unmarshal(stringReader);
    }
}
